import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import {toast} from 'nc-lightapp-front'
import getDisabledIndex from "./getDisabledIndex";
import updateSubButtonStatus from "./updateSubButtonStatus";
//hi_psndoc_deptchg/hi_psndoc_part/hi_psndoc_dimission取行上的pk_psnjob,其他的取pk_psndoc_sub
const mainTables = ['hi_psndoc_deptchg', 'hi_psndoc_part', 'hi_psndoc_dimission'];

export default function insertLine() {
    const {areaCode} = this.state;
    const {isIncHisPartJob, selectedPsn, orgVal, editTable, disabledAllArea, func_type} = this.props;

    let index = editTable.getClickRowIndex(areaCode);
    if (index === undefined || index === null || index === '') {
        toast({color: "warning", content: this.props.json['hi6007-000114']});/* 国际化处理： 请选择要插入的数据*/
        return;
    }
    index = index.index;
    const pkField = mainTables.indexOf(areaCode) > -1 ? 'pk_psnjob' : 'pk_psndoc_sub';
    const allRows = editTable.getAllRows(areaCode);
    const pk_sub = allRows[index].values[pkField].value;

    let postData = {
        areaCode,
        func_type,
        isIncHisPartJob,
        pk_org: orgVal.refpk,
        pk_psnjob: selectedPsn,
        pk_sub
    };

    return proFetch({
        url: '/nccloud/hrtrn/pub/RecordsInsertLineAction.do',
        body: postData,
    })
        .then((res) => {
            if (res.success) {
                editTable.addRow(areaCode, index, true, res.data.grid[areaCode].rows[0].values);
                const disabledIndex = getDisabledIndex(index, allRows.length + 1);
                disabledIndex && editTable.setEditableRowByIndex(areaCode, disabledIndex, false);
                disabledAllArea(true);
                updateSubButtonStatus.call(this, true);
                this.props.button.setButtonDisabled({
                    'delete': true,
                    'edit': true,
                    'change': true
                })
            }
        });
}
